package com.atguigu.yygh.msm.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.atguigu.yygh.common.utils.HttpUntils;
import com.atguigu.yygh.msm.service.MsmService;
import com.atguigu.yygh.msm.utils.ConstantPropertiesUtils;
import com.atguigu.yygh.vo.msm.MsmVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;

@Service
public class MsmServiceImpl implements MsmService {
    @Autowired
    private RedisTemplate<String,String> redisTemplate;

    @Override
    public boolean send(String phone, String code,String Content,String user,String pwd) throws UnsupportedEncodingException, MalformedURLException {
        //判断手机号是否为空
        if(StringUtils.isEmpty(phone)) {
            return false;
        }
        //整合树仁信息科技短信服务
        URL url = null;
        String send_content = URLEncoder.encode(Content.replaceAll("<br/>", " "), "UTF-8");//发送内容
        url = new URL("https://api.sr-sure.com:6887/SMSSend.aspx?Account=" + user + "&Password=" + pwd + "&Mobile=" + phone + "&Content=" + send_content);
        BufferedReader in = null;
//        int inputLine = 0;
        try {
            System.out.println("开始发送短信手机号码为 ：" + phone);
            in = new BufferedReader(new InputStreamReader(url.openStream()));
            System.out.println("服务器返回"+in.readLine());
            return true;
        } catch (Exception e) {
            return false;
        }
    }

//    //mq发送短信封装
    @Override
    public boolean send(MsmVo msmVo) {
        System.out.println("走到了模板短信的位置了");
        //判断手机号是否是空，不为空才发送
        if(!StringUtils.isEmpty(msmVo.getPhone())) {
            String url = "https://api.sr-sure.com:6890/SendSMSByTemplate.aspx";
            String account = "SRJK18512";//必须
            String password = "lzq@1102";//必须
            String extnum = "";//必须
            String tplid = "23";//必须

            boolean isSend = this.send(msmVo.getPhone(), account,password,extnum,tplid,msmVo.getParam(),url);
            return isSend;
        }
        return false;
    }

    private boolean send(String phone,String account,String password,
                         String extnum,String tplid, Map<String,Object> param,String url) {
        //判断手机号是否为空
        if(StringUtils.isEmpty(phone)) {
            return false;
        }

        Map<String, Object> data = new HashMap<>();
        data.put("phones", phone);
        data.put("account", account);
        data.put("password", password);
        data.put("extnum", extnum);
        data.put("tplid", tplid);
        data.put("params", param);
        String s = HttpUntils.postJson(url, JSONObject.toJSONString(data));
        System.out.println("树仁信息短信接口返回的数据是："+s);

        return true;
    }
}
